/**
 * 
 */
package com.idea.auction.controller;



import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.idea.auction.constant.Const;
import com.idea.auction.model.Admin;
import com.idea.auction.model.Live;
import com.idea.auction.service.LiveService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/live",viewPath="/pages")
public class LiveController extends BaseController {
	private static final Logger log = Logger.getLogger(LiveController.class);
	@Service
	private LiveService liveService;
	/**
	 * 跳转到直播页
	 */
	@RequiresPermissions("live/toLiveList")
	public void toLiveList(){
		getNavigation();
		render("live/live-list.html");
	}

	
	/**
	 * 直播间编辑页面
	 */
	public void toEditLive(){
		//getNavigation();
		Live live = new Live();
		
		if(!StrKit.isBlank(this.getPara("id"))){
			live = liveService.getLiveById(this.getPara("id"));
		}
		//对象
		setAttr("live", live);
		
		render("live/live-edit.html");
	}
	
	/**
	 * 获取直播分页
	 */
	public void getLiveList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		super.setPage(liveService.getPage(curPage));
	}


	/**
	 * 添加直播间
	 */
	public void addLive(){
		String live_picture = super.uploadImg("live_picture");
		
		Live live=new Live();
		Admin admin = (Admin) getSession().getAttribute("admin");
		
		live.set("live_name", this.getPara("live_name"));
		live.set("live_url", this.getPara("live_url"));
		live.set("live_agent", this.getPara("live_agent"));
		if(!StrKit.isBlank(live_picture)){
			live.set("live_picture", live_picture);
		}
		live.set("create_name", admin.get("name"));
		live.set("create_date", new Date());
		if(liveService.add(live)){
			msgAdd();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
	}
	
	/**
	 * 编辑直播间
	 */
	public void editLive(){
        String live_picture = super.uploadImg("live_picture");
		
		Live live=new Live();
		Admin admin = (Admin) getSession().getAttribute("admin");
		
		live.set("id",this.getPara("id"));
		live.set("live_name", this.getPara("live_name"));
		live.set("live_url", this.getPara("live_url"));
		live.set("live_agent", this.getPara("live_agent"));
		if(!StrKit.isBlank(live_picture)){
			live.set("live_picture", live_picture);
		}
		if(liveService.update(live)){
			msgUpdate();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
	}
	
	
	/**
	 * 删除直播间
	 */
	public void delLive(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			liveService.delete(id);
		}
		
		msgSuccess();
	}
	
}	